'use strict';

const vinylFs = require('vinyl-fs');
const webpack = require('../gulp/plugins/webpack');
const named = require('vinyl-named');
const conf = require('../../webpack.config.js');
const util = require('util');

function bulid(src, options, dest, resolve, reject) {
    vinylFs.src(src)
        .pipe(named())
        .pipe(webpack(Object.assign(true, {}, conf, options)))
        .on('error', function(err) {
            if (util.isFunction(reject)) {
                reject(err);
            }
        })
        .on('end', function() {
            if (util.isFunction(resolve)) {
                setTimeout(function() {
                    resolve();
                }, 100);
            }
        })
        .pipe(vinylFs.dest(dest));
}

exports.production = function(src, dest, resolve, reject) {
    bulid(src, {
        watch: false,
        devtool: '#inline-source-map'
    }, dest, resolve, reject);
};

exports.dev = function(src, dest, resolve, reject) {
    bulid(src, {
        watch: true
    }, dest, resolve, reject);
};